package 第二周;

import java.util.*;

public class 字母异位词分组 {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        HashMap<String, List<String>> hashMap = new HashMap<>();
        for (int i = 0; i < strs.length; i++) {
            char[] ar = strs[i].toCharArray();
            Arrays.sort(ar);
            if (!hashMap.containsKey(String.valueOf(ar))){
                ArrayList<String> list = new ArrayList<>();
                list.add(strs[i]);
                hashMap.put(String.valueOf(ar),list);
            }else {
                // 存在
                List<String> list = hashMap.get(String.valueOf(ar));
                list.add(strs[i]);
                hashMap.put(String.valueOf(ar),list);
            }
        }
        for (Map.Entry<String, List<String>> stringListEntry : hashMap.entrySet()) {
            result.add(stringListEntry.getValue());
        }
        return result;
    }

//    public static void main(String[] args) {
//        String[] strings = {"bac", "adc","abc"};
//        System.out.println(groupAnagrams());
//    }
}
